#tag Class
Protected Class GameFacade
	#tag Method, Flags = &h0
		Sub AddTileAtPosition(x as integer, y as integer)
		  '(floor(x/app.TILESIZE)*app.TILESIZE - Window1.xScroll, floor(y/app.TILESIZE)*app.TILESIZE - Window1.yScroll)
		  dim position as Coordinates
		  
		  position = GetAbsolutePositionByXy(x,y)
		  
		  if GetPositionIsFree(position) then
		    if game.AddTileAtPosition(GetCurrentTile, position) then
		      call GetNextTile
		      winMain.canBoard.SetTiles(GetTiles)
		      GetPossibleCoordinatesForTile
		      winMain.canBoard.Refresh
		      winMain.canNext.Refresh
		    end if
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetPositionIsFree(position as Coordinates) As boolean
		  return game.GetPositionIsFree(position)
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub GetPossibleCoordinatesForTile()
		  winMain.canBoard.currentPossiblePositions = game.GetPossibleCoordinatesForTile
		  
		  
		  
		  //MsgBox output
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetNextTile() As Tile
		  
		  return game.GetNextTile
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetRemainingTiles() As Tile()
		  return game.GetRemainingTiles
		  
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetCurrentPlayerName() As String
		  return game.getCurrentPlayerName
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub StartNewGame()
		  winmain.mode = 1
		  if self.game <> nil then 'there is already a game running - ask the player if he really wants to create a new game
		    dim modDlg as new MessageDialog
		    dim btn as MessageDialogButton
		    modDlg.Icon = MessageDialog.GraphicQuestion
		    modDlg.ActionButton.Caption = "Yes"
		    modDlg.CancelButton.Visible = True
		    modDlg.CancelButton.Caption = "No"
		    modDlg.Message = "Do you want to start a new game?"
		    modDlg.Explanation = "All moves will be discarded!"
		    btn = modDlg.ShowModalWithin(winMain)
		    
		    select case btn
		    case modDlg.ActionButton
		      self.game = self.game.GetNewGameInstance
		      'this is odd as it should be self.game = game.ConcreteGame.Instance but it doesn't work the other way
		      'maybe its a Realbasic bug...
		      modNewGame.ShowModalWithin(winmain)
		      
		      'winMain.canBoard.SetTiles(GetTiles)
		      'GetPossibleCoordinatesForTile
		      'winMain.canBoard.Refresh
		      'winMain.canNext.Refresh
		    case modDlg.CancelButton
		      exit sub
		    end select
		  else
		    self.game = self.game.Instance
		    'this is odd as it should be self.game = game.ConcreteGame.Instance but it doesn't work the other way
		    'maybe its a Realbasic bug...
		    
		    modNewGame.ShowModalWithin(winMain)
		    
		    'winMain.canBoard.SetTiles(GetTiles)
		    'GetPossibleCoordinatesForTile
		    'winMain.canBoard.Refresh
		    'winMain.canNext.Refresh
		    
		  end if
		  
		  
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetTiles() As Tile()
		  return game.GetTiles
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GameExists() As boolean
		  if game = nil then
		    return false
		  else
		    return true
		  end if
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub RotateClockwise()
		  game.RotateClockwise
		  GetPossibleCoordinatesForTile
		  winMain.canBoard.Refresh
		  winMain.canNext.Refresh
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub RotateCounterclockwise()
		  game.RotateCounterclockwise
		  GetPossibleCoordinatesForTile
		  winMain.canBoard.Refresh
		  winMain.canNext.Refresh
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function GetCurrentTile() As Tile
		  return game.GetCurrentTile
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetupNewGame(dataArray() as playerdata)
		  dim i as integer
		  
		  for i = 0 to ubound(dataArray)
		    MsgBox dataArray(i).name
		  next i
		  
		  
		  
		  
		  
		  winMain.canBoard.SetTiles(GetTiles)
		  GetPossibleCoordinatesForTile
		  winMain.canBoard.Refresh
		  winMain.canNext.Refresh
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub ShowAIGame()
		  if IsViewable = true then
		    winMain.canBoard.SetTiles(GetTiles)
		    winMain.canBoard.Refresh
		    winmain.mode = 2
		    winAIRun.edtAILog.Text = game.ReadLog
		  end if
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub SetAIGame(g as game . concreteGame)
		  game = g
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub AddAIGame(g as game . concreteGame)
		  gameList.Append g
		End Sub
	#tag EndMethod

	#tag Method, Flags = &h0
		Function SetAIGameByIndex(index as integer) As game.ConcreteGame
		  if IsViewable = true then
		    game = gameList(index)
		  end if
		  
		  return game
		End Function
	#tag EndMethod

	#tag Method, Flags = &h0
		Sub Reset()
		  game = nil
		  Redim gameList(-1)
		  
		End Sub
	#tag EndMethod


	#tag Property, Flags = &h21
		Private game As game.concreteGame
	#tag EndProperty

	#tag Property, Flags = &h21
		Private gameList() As game.concreteGame
	#tag EndProperty

	#tag Property, Flags = &h0
		IsViewable As boolean = false
	#tag EndProperty


	#tag ViewBehavior
		#tag ViewProperty
			Name="Name"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Index"
			Visible=true
			Group="ID"
			InitialValue="-2147483648"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Super"
			Visible=true
			Group="ID"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Left"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="Top"
			Visible=true
			Group="Position"
			InitialValue="0"
			InheritedFrom="Object"
		#tag EndViewProperty
		#tag ViewProperty
			Name="IsViewable"
			Group="Behavior"
			InitialValue="false"
			Type="boolean"
		#tag EndViewProperty
	#tag EndViewBehavior
End Class
#tag EndClass
